<html>
<head><meta charset="utf-8"><title>Access to Stdin buffer · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html">Access to Stdin buffer</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242461681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242461681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> notoria <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242461681">(Jun 12 2021 at 17:18)</a>:</h4>
<p>Hello, I would like to have an access to the standard input buffer. The <code>StdinLock</code> uses <code>BufReader</code>, so the buffer could be accessed. If the buffer is empty <code>fill_buf</code> will block while I would like a non-blocking access. Is it possible?</p>



<a name="242461902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242461902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242461902">(Jun 12 2021 at 17:24)</a>:</h4>
<p>I think you will have to use an OS api for that. (on unix I guess dup the stdin fd, set it to non-blocking and then use the read syscall)</p>



<a name="242462192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> notoria <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462192">(Jun 12 2021 at 17:33)</a>:</h4>
<p>It doesn't seem to be required to use the OS API since <code>StdinLock</code> have a buffer. The only way to access the buffer is <code>fill_buf</code> which will block for input if the buffer is empty. I would like to access the buffer without requesting for more input, just like <code>BufReader</code>.</p>



<a name="242462240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462240">(Jun 12 2021 at 17:34)</a>:</h4>
<p>What if the buffer is empty, but the user did provide input?</p>



<a name="242462305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> notoria <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462305">(Jun 12 2021 at 17:37)</a>:</h4>
<p>I'm abstracting the input, if the buffer is empty then I can read some default data.</p>



<a name="242462371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462371">(Jun 12 2021 at 17:39)</a>:</h4>
<p>You mean you want <code>StdinLock</code> to return fake data? For testing purposes?</p>



<a name="242462455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> notoria <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462455">(Jun 12 2021 at 17:41)</a>:</h4>
<p>No.</p>



<a name="242462466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> notoria <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462466">(Jun 12 2021 at 17:41)</a>:</h4>
<p>If the input isn't in the buffer then where could it be?</p>



<a name="242462472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462472">(Jun 12 2021 at 17:42)</a>:</h4>
<p>In the OS buffer.</p>



<a name="242462542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462542">(Jun 12 2021 at 17:42)</a>:</h4>
<p>A pipe has a buffer associated with it in the kernel. When you write to a pipe, you won't block until this buffer is completely filled. When you read from it, you won't block until it is completely empty.</p>



<a name="242462619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462619">(Jun 12 2021 at 17:44)</a>:</h4>
<p>The buffer in <code>StdinLock</code> is a completely separate buffer to reduce the amount of slow syscalls.</p>



<a name="242462630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> notoria <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462630">(Jun 12 2021 at 17:44)</a>:</h4>
<p>I understand, even if the input is in OS buffer. I can still decide to retrieve (possible blocking) or read something else.</p>



<a name="242462648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Access%20to%20Stdin%20buffer/near/242462648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Access.20to.20Stdin.20buffer.html#242462648">(Jun 12 2021 at 17:45)</a>:</h4>
<p>Correct, but you need to set the fd you read from to non-blocking mode and use the os read api to not block on any attempt to read from stdin.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>